package com.ch.service;

import cn.hutool.json.JSONUtil;
import com.ch.Result;
import com.ch.client.UserClient;
import com.ch.dao.OrderDao;
import com.ch.entity.Order;
import com.ch.entity.User;
import com.ch.entity.appproprity;
import org.aspectj.weaver.ast.Or;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class OrderService {
    @Autowired
    private UserClient userClient;
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private appproprity appproprity;
    public boolean save(Order order){
        boolean f=false;
        System.out.println("orderserviceZ："+order);
        orderDao.insert(order);
        User user = order.getUser();
        Map map=new HashMap();
        map.put("user",JSONUtil.toJsonStr(user));
        map.put("id",order.getId());
        rabbitTemplate.convertAndSend(appproprity.getQueuename(),map);
        if (order.getId()>0){
            f=true;
        }
        return f;
    }

    public Order getOrder(int id,String token){
        Order order = orderDao.selectById(id);
        User user = userClient.getById(id,token);
        order.setUser(user);
        return order;
    }
    public boolean updateUid(Map<String,Integer> map){
        boolean f=false;
        f = orderDao.updateUid(map);
        return f;
    }
}
